<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<title>Peity &bull; progressive &lt;svg&gt; pie charts</title>
<link href="docs/style.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="jquery.peity.js"></script>
<script>
$(function() {
  // Just the defaults.
  $("span.pie").peity("pie")
  $(".line").peity("line")
  $(".bar").peity("bar")

  $(".bar-colours-1").peity("bar", {
    fill: ["red", "green", "blue"]
  })

  $(".bar-colours-2").peity("bar", {
    fill: function(value) {
      return value > 0 ? "green" : "red"
    }
  })

  $(".bar-colours-3").peity("bar", {
    fill: function(_, i, all) {
      var g = parseInt((i / all.length) * 255)
      return "rgb(255, " + g + ", 0)"
    }
  })

  $(".pie-colours-1").peity("pie", {
    fill: ["cyan", "magenta", "yellow", "black"]
  })

  $(".pie-colours-2").peity("pie", {
    fill: function(_, i, all) {
      var g = parseInt((i / all.length) * 255)
      return "rgb(255, " + g + ", 0)"
    }
  })

  // Using data attributes
  $(".pie span").peity("pie")

  // Simple evented example.
  $("select").change(function() {
    var text = $(this).val() + "/" + 5

    $(this)
      .siblings("span.graph")
      .text(text)
      .change()

    $("#notice").text("Chart updated: " + text)
  }).change()

  $("span.graph").peity("pie")

  // Updating charts.
  var updatingChart = $(".updating-chart").peity("line", { width: 64 })

  setInterval(function() {
    var random = Math.round(Math.random() * 10)
    var values = updatingChart.text().split(",")
    values.shift()
    values.push(random)

    updatingChart
      .text(values.join(","))
      .change()
  }, 1000)
})
</script>
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-117680-14']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
</head>
<body>
<div id="container">
  <h1>Peity</h1>

  <iframe src="http://ghbtns.com/github-btn.html?user=benpickles&repo=peity&type=watch&count=true"
    allowtransparency="true" frameborder="0" scrolling="0" width="110" height="20"></iframe>

  <iframe src="http://ghbtns.com/github-btn.html?user=benpickles&repo=peity&type=fork&count=true"
    allowtransparency="true" frameborder="0" scrolling="0" width="95" height="20"></iframe>

  <p>Peity (sounds like deity) is a simple <a href="http://jquery.com/">jQuery</a>
    plugin that converts an element's content into a simple <code>&lt;svg&gt;</code>
    mini pie <span class="pie">2/5</span>
    line <span class="line">5,3,9,6,5,9,7,3,5,2</span>
    or bar chart <span class="bar">5,3,9,6,5,9,7,3,5,2</span> and is
    compatible with any browser that supports <code>&lt;svg&gt;</code>:
    Chrome, Firefox, IE9+, Opera, Safari.</p>

  <nav>
    <p>
      Read about <a href="#pie-charts">pie charts</a>,
      <a href="#line-charts">line charts</a>,
      <a href="#bar-charts">bar charts</a>,
      <a href="#dynamic-colours">setting colours dynamically</a>,
      <a href="#updating-charts">updating charts</a>,
      <a href="#custom-charts">custom charts</a>,
      <a href="#data-attributes">data-* attributes</a> and
      <a href="#events">events</a>,
      <a href="#defaults">default settings</a>.
    </p>
  </nav>

  <h2>Download <span class="version">version 2.0.3</span></h2>

  <dl>
    <dt>Uncompressed <span class="size-js">7.2Kb</span></dt>
    <dd><a href="jquery.peity.js">jquery.peity.js</a></dd>
    <dt>Minified <span class="size-min">3.3Kb</span> (+gzipped <span class="size-gz">1.5Kb</span>)</dt>
    <dd><a href="jquery.peity.min.js">jquery.peity.min.js</a></dd>
    <dt>Source</dt>
    <dd><a href="https://github.com/benpickles/peity">github.com/benpickles/peity</a></dd>
  </dl>

  <a href="https://github.com/benpickles/peity" id="fork-me"><img src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>

  <h2 id="pie-charts">Pie Charts</h2>

  <p>Simply call <code>peity("pie")</code> on a jQuery selection. There are
    two subtly different pie chart semantics, a "/" delimiter is assumed to
    mean "three out of five" and only the first two values will be drawn,
    otherwise all of the values are included in the chart and the total is the
    sum of all values.</p>

  <p>You can also pass <code>delimiter</code>, <code>diameter</code>,
    <code>fill</code>, <code>height</code> and <code>width</code> options.
    Setting a diameter is a shortcut for setting the width and height to the
    same value.</p>

  <p>
    <span class="pie">1/5</span>
    <span class="pie">226/360</span>
    <span class="pie">0.52/1.561</span>
    <span class="pie">1,4</span>
    <span class="pie">226,134</span>
    <span class="pie">0.52,1.041</span>
    <span class="pie">1,2,3,2,2</span>
  </p>

  <div class="example">
    <h4>HTML</h4>

    <pre><code class="html">&lt;span class=&quot;pie&quot;&gt;1/5&lt;/span&gt;
&lt;span class=&quot;pie&quot;&gt;226/360&lt;/span&gt;
&lt;span class=&quot;pie&quot;&gt;0.52/1.561&lt;/span&gt;
&lt;span class=&quot;pie&quot;&gt;1,4&lt;/span&gt;
&lt;span class=&quot;pie&quot;&gt;226,134&lt;/span&gt;
&lt;span class=&quot;pie&quot;&gt;0.52,1.041&lt;/span&gt;
&lt;span class=&quot;pie&quot;&gt;1,2,3,2,2&lt;/span&gt;</code></pre>

    <h4>JavaScript</h4>

    <pre><code class="javascript">$("span.pie").peity("pie")</code></pre>
  </div>

  <h2 id="line-charts">Line Charts</h2>

  <p>Line charts work on a comma-separated list of digits. Line charts can
    take the following options: <code>delimiter</code>, <code>fill</code>,
    <code>height</code>, <code>max</code>, <code>min</code>,
    <code>stroke</code>, <code>strokeWidth</code> and <code>width</code>.</p>

  <p>
    <span class="line">5,3,9,6,5,9,7,3,5,2</span>
    <span class="line">5,3,2,-1,-3,-2,2,3,5,2</span>
    <span class="line">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span>
  </p>

  <div class="example">
    <h4>HTML</h4>
    <pre><code class="html">&lt;span class="line"&gt;5,3,9,6,5,9,7,3,5,2&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;5,3,2,-1,-3,-2,2,3,5,2&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;0,-3,-6,-4,-5,-4,-7,-3,-5,-2&lt;/span&gt;</code></pre>

    <h4>JavaScript</h4>
    <pre><code class="javascript">$(".line").peity("line")</code></pre>
  </div>

  <h2 id="bar-charts">Bar Charts</h2>

  <p>Bar charts work in the same way as line charts and take the following
    options: <code>delimiter</code>, <code>fill</code>, <code>gap</code>,
    <code>height</code>, <code>max</code>, <code>min</code> and
    <code>width</code>.</p>

  <p>
    <span class="bar">5,3,9,6,5,9,7,3,5,2</span>
    <span class="bar">5,3,2,-1,-3,-2,2,3,5,2</span>
    <span class="bar">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span>
  </p>

  <div class="example">
    <h4>HTML</h4>
    <pre><code class="html">&lt;span class="bar"&gt;5,3,9,6,5,9,7,3,5,2&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;5,3,2,-1,-3,-2,2,3,5,2&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;0,-3,-6,-4,-5,-4,-7,-3,-5,-2&lt;/span&gt;</code></pre>

    <h4>JavaScript</h4>
    <pre><code class="javascript">$(".bar").peity("bar")</code></pre>
  </div>

  <h2 id="dynamic-colours">Setting Colours Dynamically</h2>

  <p>Bar and pie chart colours can be defined dynamically based on the values
    of the chart. When passing an array its values are cycled, when passing a
    function it is called once for each value allowing you to define each
    bar's colour. The callback is invoked with the value, its index, and the
    full array of values - the same arguments as the callback for
    <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description">Array#forEach</a></code>.</p>

  <p>
    <span class="bar-colours-1">5,3,9,6,5,9,7,3,5,2</span>
    <span class="bar-colours-2">5,3,2,-1,-3,-2,2,3,5,2</span>
    <span class="bar-colours-3">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span>
    <span class="pie-colours-1">4,7,6,5</span>
    <span class="pie-colours-2">5,3,9,6,5</span>
  </p>

  <div class="example">
    <h4>HTML</h4>
    <pre><code class="html">&lt;span class="bar-colours-1"&gt;5,3,9,6,5,9,7,3,5,2&lt;/span&gt;
&lt;span class=&quot;bar-colours-2&quot;&gt;5,3,2,-1,-3,-2,2,3,5,2&lt;/span&gt;
&lt;span class=&quot;bar-colours-3&quot;&gt;0,-3,-6,-4,-5,-4,-7,-3,-5,-2&lt;/span&gt;
&lt;span class=&quot;pie-colours-1&quot;&gt;4,7,6,5&lt;/span&gt;
&lt;span class=&quot;pie-colours-2&quot;&gt;5,3,9,6,5&lt;/span&gt;</code></pre>

    <h4>JavaScript</h4>
    <pre><code class="javascript">$(".bar-colours-1").peity("bar", {
  fill: ["red", "green", "blue"]
})

$(".bar-colours-2").peity("bar", {
  fill: function(value) {
    return value > 0 ? "green" : "red"
  }
})

$(".bar-colours-3").peity("bar", {
  fill: function(_, i, all) {
    var g = parseInt((i / all.length) * 255)
    return "rgb(255, " + g + ", 0)"
  }
})

$(".pie-colours-1").peity("pie", {
  fill: ["cyan", "magenta", "yellow", "black"]
})

$(".pie-colours-2").peity("pie", {
  fill: function(_, i, all) {
    var g = parseInt((i / all.length) * 255)
    return "rgb(255, " + g + ", 0)"
  }
})</code></pre>
  </div>

  <h2 id="updating-charts">Updating Charts</h2>

  <p>Charts can be updated by changing the the jQuery selection's text content
    and calling <code>change()</code> on it. The chart will be redrawn with
    the same options that were originally passed to it.</p>

  <p>
    <span class="updating-chart">5,3,9,6,5,9,7,3,5,2,5,3,9,6,5,9,7,3,5,2</span>
  </p>

  <div class="example">
    <h4>HTML</h4>
    <pre><code class="html">&lt;span class="updating-chart"&gt;5,3,9,6,5,9,7,3,5,2,5,3,9,6,5,9,7,3,5,2&lt;/span&gt;</code></pre>

    <h4>JavaScript</h4>
    <pre><code class="javascript">var updatingChart = $(".updating-chart").peity("line", { width: 64 })

setInterval(function() {
  var random = Math.round(Math.random() * 10)
  var values = updatingChart.text().split(",")
  values.shift()
  values.push(random)

  updatingChart
    .text(values.join(","))
    .change()
}, 1000)</code></pre>
  </div>

  <h2 id="custom-charts">Custom Charts</h2>

  <p>You can easily add your own custom chart by registering it with
    Peity with name, defaults and the draw function which receives an options
    object.</p>

  <pre><code class="javascript">$.fn.peity.add("custom", {
    fill: "#FFCC00"
  }, function(opts) {
    ...
  }
)</code></pre>

  <h2 id="data-attributes">data-* attributes</h2>

  <p>If you use data attributes, Peity will apply their values to the defaults
    with the same names automatically. Options passed to the
    <code>peity()</code> function take precedence over data-* attributes.</p>

  <p class="pie">
    <span data-diameter="40" data-fill='["red", "#eeeeee"]'   >1/7</span>
    <span data-diameter="36" data-fill='["orange", "#eeeeee"]'>2/7</span>
    <span data-diameter="32" data-fill='["yellow", "#eeeeee"]'>3/7</span>
    <span data-diameter="28" data-fill='["green", "#eeeeee"]' >4/7</span>
    <span data-diameter="24" data-fill='["blue", "#eeeeee"]'  >5/7</span>
    <span data-diameter="20" data-fill='["indigo", "#eeeeee"]'>6/7</span>
    <span data-diameter="16" data-fill='["violet", "#eeeeee"]'>7/7</span>
  </p>

  <div class="example">
    <h4>HTML</h4>

    <pre><code class="html">&lt;span data-diameter=&quot;40&quot; data-fill=&#x27;[&quot;red&quot;, &quot;#eeeeee&quot;]&#x27;   &gt;1/7&lt;/span&gt;
&lt;span data-diameter=&quot;36&quot; data-fill=&#x27;[&quot;orange&quot;, &quot;#eeeeee&quot;]&#x27;&gt;2/7&lt;/span&gt;
&lt;span data-diameter=&quot;32&quot; data-fill=&#x27;[&quot;yellow&quot;, &quot;#eeeeee&quot;]&#x27;&gt;3/7&lt;/span&gt;
&lt;span data-diameter=&quot;28&quot; data-fill=&#x27;[&quot;green&quot;, &quot;#eeeeee&quot;]&#x27; &gt;4/7&lt;/span&gt;
&lt;span data-diameter=&quot;24&quot; data-fill=&#x27;[&quot;blue&quot;, &quot;#eeeeee&quot;]&#x27;  &gt;5/7&lt;/span&gt;
&lt;span data-diameter=&quot;20&quot; data-fill=&#x27;[&quot;indigo&quot;, &quot;#eeeeee&quot;]&#x27;&gt;6/7&lt;/span&gt;
&lt;span data-diameter=&quot;16&quot; data-fill=&#x27;[&quot;violet&quot;, &quot;#eeeeee&quot;]&#x27;&gt;7/7&lt;/span&gt;</code></pre>
    <h4>JavaScript</h4>

    <pre><code class="javascript">$(&quot;.pie span&quot;).peity(&quot;pie&quot;)</code></pre>
  </div>

  <h2 id="events">Events</h2>

  <p>Peity adds a "change" event trigger to your graph elements, so if you
    update their data your can regenerate one or more charts by triggering
    <code>change()</code> on them.</p>

  <ul>
    <li>
      <span class="graph"></span>
      <select>
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4" selected>4</option>
        <option value="5">5</option>
      </select>
    </li>
    <li>
      <span class="graph"></span>
      <select>
        <option value="0">0</option>
        <option value="1" selected>1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
      </select>
    </li>
    <li>
      <span class="graph"></span>
      <select>
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3" selected>3</option>
        <option value="4">4</option>
        <option value="5">5</option>
      </select>
    </li>
  </ul>

  <p id="notice">Nothing's happened yet.</p>

  <div class="example">
    <h4>HTML</h4>

    <pre><code class="html">&lt;ul&gt;
  &lt;li&gt;
    &lt;span class="graph"&gt;&lt;/span&gt;
    &lt;select&gt;
      &lt;option value="0"&gt;0&lt;/option&gt;
      &lt;option value="1"&gt;1&lt;/option&gt;
      &lt;option value="2"&gt;2&lt;/option&gt;
      &lt;option value="3"&gt;3&lt;/option&gt;
      &lt;option value="4" selected&gt;4&lt;/option&gt;
      &lt;option value="5"&gt;5&lt;/option&gt;
    &lt;/select&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;span class="graph"&gt;&lt;/span&gt;
    &lt;select&gt;
      &lt;option value="0"&gt;0&lt;/option&gt;
      &lt;option value="1" selected&gt;1&lt;/option&gt;
      &lt;option value="2"&gt;2&lt;/option&gt;
      &lt;option value="3"&gt;3&lt;/option&gt;
      &lt;option value="4"&gt;4&lt;/option&gt;
      &lt;option value="5"&gt;5&lt;/option&gt;
    &lt;/select&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;span class="graph"&gt;&lt;/span&gt;
    &lt;select&gt;
      &lt;option value="0"&gt;0&lt;/option&gt;
      &lt;option value="1"&gt;1&lt;/option&gt;
      &lt;option value="2"&gt;2&lt;/option&gt;
      &lt;option value="3" selected&gt;3&lt;/option&gt;
      &lt;option value="4"&gt;4&lt;/option&gt;
      &lt;option value="5"&gt;5&lt;/option&gt;
    &lt;/select&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p id="notice"&gt;Nothing's happened yet.&lt;/p&gt;</code></pre>

    <h4>JavaScript</h4>

    <pre><code class="javascript">$("select").change(function() {
  $(this)
    .siblings("span.graph")
    .text($(this).val() + "/" + 5).change()
}).change()

var chartUpdate = function(event, value, max) {
  $("#notice").text(
    "Chart updated: " + value + "/" + max
  )
}

$("span.graph")
  .peity("pie")
  .bind("change.peity", chartUpdate)</code></pre>
  </div>

  <h2 id="defaults">Default Settings</h2>

  <p>Defaults can be overridden globally like so:</p>

  <pre><code class="javascript">$.fn.peity.defaults.pie = {
  delimiter: null,
  diameter: 16,
  fill: ["<span style="background:#ff9900">#ff9900</span>", "<span style="background:#fff4dd">#fff4dd</span>", "<span style="background:#ffd592">#ffd592</span>"],
  height: null,
  width: null
}

$.fn.peity.defaults.line = {
  delimiter: ",",
  fill: "<span style="background:#c6d9fd">#c6d9fd</span>",
  height: 16,
  max: null,
  min: 0,
  stroke: "<span style="background:#4d89f9">#4d89f9</span>",
  strokeWidth: 1,
  width: 32
}

$.fn.peity.defaults.bar = {
  delimiter: ",",
  fill: ["<span style="background:#4d89f9">#4d89f9</span>"],
  gap: 1,
  height: 16,
  max: null,
  min: 0,
  width: 32
}</code></pre>

  <footer>
    <p>
      &copy; 2009-2014 <a href="http://benpickles.com/">Ben Pickles</a>.
      <a href="https://github.com/benpickles/peity/blob/master/MIT-LICENCE">MIT LICENCE</a>.
    </p>
  </footer>
</div>
</body>
</html>
